
var utils = (function () {

    var __class = function () { this.__self = this; };
    __class.prototype.getSelf = function (str) {
        return this.__self;
    }

    //判断有无class
    __class.prototype.hasClass = function(ele, cls) {
        return ele.className.match(new RegExp("(\\s|^)" + cls + "(\\s|$)"));
    }

    //添加class
    __class.prototype.addClass = function(ele, cls) {
        if (!new __class().hasClass(ele, cls)) ele.className += " " + cls;
    }

    //删除class
    __class.prototype.removeClass = function(ele, cls) {
        if (new __class().hasClass(ele, cls)) {
            let reg = new RegExp("(\\s|^)" + cls + "(\\s|$)");
            ele.className = ele.className.replace(reg, " ");
        }
    }

    //生成从minNum到maxNum的随机数
    __class.prototype.randomNum = function(minNum,maxNum){ 
        switch(arguments.length){ 
            case 1: 
                return parseInt(Math.random()*minNum+1,10); 
            break; 
            case 2: 
                return parseInt(Math.random()*(maxNum-minNum+1)+minNum,10); 
            break; 
                default: 
                    return 0; 
                break; 
        } 
    } 

    //本地存储对象
    __class.prototype.local = null;
    //初始化本地存储，callback为必选 操作存储后的回调函数，存在flag则为sessionStorage
    __class.prototype.init = function(callback, flag){
        if (!localStorage) {
            alert("您的浏览器不支持本地存储！");
            return;
        }
        if (window && window.addEventListener) {
            this.__self.local = flag ? window.sessionStorage : window.localStorage;
            window.addEventListener('storage',function(e){
                callback(e);
                /*
                    e.key            键值对的键
                    e.oldValue       修改之前的value
                    e.newValue       修改之后的value
                    e.url            触发改动的页面url
                    e.StorageArea    发生改变的Storage
                */
            },false);
        }else{
            window.attachEvent("onstorage", function(e){
                callback(e);
                /*
                    e.key            键值对的键
                    e.oldValue       修改之前的value
                    e.newValue       修改之后的value
                    e.url            触发改动的页面url
                    e.StorageArea    发生改变的Storage
                */
            });
        }
    }
    //获取本地存储
    __class.prototype.getStorage = function(name){   //不存在返回null
        let g = this.__self.local.getItem(name);
        if(!g){return null;}
        return JSON.parse(g);
    }
    //设置本地存储
    __class.prototype.setStorage = function(name, value) {
        let d = this.__self.local.setItem(name, JSON.stringify(value));
        return d;
    }
    //删除本地存储
    __class.prototype.deleteStorage = function(name) {   //不存在返回null
        let g = JSON.stringify(this.__self.local.getItem(name)) || null;
        if (!g) {
            return null;
        }
        return this.__self.local.removeItem(name);
    }
    //清除所有本地存储
    __class.prototype.clearStorage = function() {
        return this.__self.local.clear();
    }
    //通过key获取本地存储
    __class.prototype.keyStorage = function(index) {
        return this.__self.local.key(index);
    }

    return new __class();
})();